package com.lmiot.xyclick.Util;

import android.content.Context;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;
import com.lmiot.xyclick.App.MyApp;
import com.youyi.yypermissionlibrary.SDK.YYPerUtils;
import java.io.File;
import java.security.MessageDigest;

public class PhoneUtil {
    public static String getIMEI(Context context) {
        if (TextUtils.isEmpty(DataUtil.getSessionID(MyApp.getContext()))) {
            return getOldID(context);
        }
        if (DataUtil.getLoginByWx(MyApp.getContext())) {
            return strToMD5_16(DataUtil.getWxID(MyApp.getContext()));
        }
        return getOldID(context);
    }

    private static String getOldID(Context context) {
        String userID = DataUtil.getUserID(context);
        if (!TextUtils.isEmpty(userID)) {
            return userID;
        }
        if (YYPerUtils.hasSD()) {
            try {
                File file = new File(Environment.getExternalStorageDirectory() + "/ZData");
                if (!file.exists()) {
                    file.mkdirs();
                }
                File file2 = new File(Environment.getExternalStorageDirectory() + "/ZData", "xydata.jar");
                if (!file2.exists()) {
                    file2.createNewFile();
                }
                String readFileToString = FileUtils.readFileToString(file2);
                if (TextUtils.isEmpty(readFileToString)) {
                    String str = "";
                    if (TextUtils.isEmpty(str)) {
                        str = TimeUtils.createID() + RandomUtil.getRandomNum(1, 999);
                    }
                    String str2 = str + "xyclick";
                    FileUtils.saveIDFile("xydata", str2);
                    DataUtil.setUserID(context, str2);
                    return str2;
                }
                if (TextUtils.isEmpty(readFileToString)) {
                    readFileToString = TimeUtils.createID() + RandomUtil.getRandomNum(1, 999);
                }
                DataUtil.setUserID(context, readFileToString);
                return readFileToString;
            } catch (Exception e) {
                e.printStackTrace();
                if (TextUtils.isEmpty(userID)) {
                    userID = TimeUtils.createID() + RandomUtil.getRandomNum(1, 999);
                }
                DataUtil.setUserID(context, userID);
                return userID;
            }
        } else {
            return TimeUtils.createID() + RandomUtil.getRandomNum(1, 999);
        }
    }

    public static String getOldVIPID(Context context) {
        if (YYPerUtils.hasSD()) {
            try {
                File file = new File(Environment.getExternalStorageDirectory() + "/ZData");
                if (!file.exists()) {
                    file.mkdirs();
                }
                File file2 = new File(Environment.getExternalStorageDirectory() + "/ZData", "xydata.jar");
                if (!file2.exists()) {
                    file2.createNewFile();
                }
                String readFileToString = FileUtils.readFileToString(file2);
                if (TextUtils.isEmpty(readFileToString)) {
                    return "";
                }
                return readFileToString;
            } catch (Exception unused) {
            }
        }
        return "";
    }

    public static String getModel() {
        try {
            return Build.MODEL;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getBrand() {
        try {
            return Build.BRAND;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getSystemVersion() {
        try {
            return Build.VERSION.RELEASE;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getAPPVersion() {
        try {
            return MyApp.getContext().getPackageManager().getPackageInfo(MyApp.getContext().getPackageName(), 0).versionName;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String getSignature(Context context) {
        try {
            return encryptionMD5(context.getPackageManager().getPackageInfo(context.getPackageName(), 64).signatures[0].toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String encryptionMD5(byte[] bArr) {
        StringBuffer stringBuffer = new StringBuffer();
        try {
            MessageDigest instance = MessageDigest.getInstance("MD5");
            instance.reset();
            instance.update(bArr);
            byte[] digest = instance.digest();
            for (int i = 0; i < digest.length; i++) {
                if (Integer.toHexString(digest[i] & -1).length() == 1) {
                    stringBuffer.append("0");
                    stringBuffer.append(Integer.toHexString(digest[i] & -1));
                } else {
                    stringBuffer.append(Integer.toHexString(digest[i] & -1));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringBuffer.toString();
    }

    public static String strToMD5_32(String str) {
        String str2 = new String();
        try {
            MessageDigest instance = MessageDigest.getInstance("MD5");
            instance.update(str.getBytes());
            byte[] digest = instance.digest();
            StringBuffer stringBuffer = new StringBuffer("");
            for (byte b : digest) {
                int i = b;
                if (b < 0) {
                    i = b + 256;
                }
                if (i < 16) {
                    stringBuffer.append("0");
                }
                stringBuffer.append(Integer.toHexString(i == 1 ? 1 : 0));
            }
            return stringBuffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return str2;
        }
    }

    private static String strToMD5_16(String str) {
        return strToMD5_32(str).substring(8, 24);
    }
}
